﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySearch;
using Kpmg.CompanyInfo;

public partial class wuc_ServiceTemplate_Editable : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    #region Ticket Data

    public void BindData(string ticketNo, string sevType, int verNo, string chargeableCode, string profileCode)
    {
        this.hfTicketNo.Value = ticketNo;
        this.hfServiceType.Value = sevType;
        this.hfVersionNo.Value = verNo.ToString();
        this.hfChargeableCode.Value = chargeableCode;
        this.hfProfileCode.Value = profileCode;
        //this.rptMain.DataSource = ServiceTemplateOM.ListServiceSteps(sevType, Convert.ToInt32(verNo));
        //this.rptMain.DataBind();
    }

    protected TicketData GetTicketData(string quesCode)
    {
        if (string.IsNullOrEmpty(this.hfTicketNo.Value))
            return new TicketData();

        return null;// TicketOM.GetTicketData(this.hfTicketNo.Value, quesCode);
    }

    public IList<TicketData> GetTicketData()
    {
        return new List<TicketData>();

        //foreach (RepeaterItem item in this.rptMain.Items)
        //{
            //var rptChild = item.FindControl("rptChild") as Repeater;
            //foreach (RepeaterItem subItem in rptChild.Items)
            //{
            //    string sevQueCode = ((HiddenField)subItem.FindControl("hfStepNo")).Value;
            //    ServiceQuestion sevQue = ServiceTemplateOM.GetServiceQuestion(sevQueCode);
            //    CheckBoxList lstCheckBox = ((CheckBoxList)subItem.FindControl("lstCheckbox"));
            //    RadioButtonList lstRadioButton = ((RadioButtonList)subItem.FindControl("lstRadioButton"));

            //    string data = String.Empty;
            //    switch (sevQue.DataType)
            //    {
            //        #region related multichoice
            //        case ServiceQuestionDataType.List:
            //            foreach (ListItem litem in lstRadioButton.Items)
            //            {
            //                if (litem.Selected)
            //                {
            //                    if (!String.IsNullOrEmpty(data))
            //                        data += AppConfigurer.TicketDataSeparator;
            //                    data += litem.Value;
            //                }
            //            }
            //            break;

            //        case ServiceQuestionDataType.EngTeam:
            //        case ServiceQuestionDataType.CheckBox:
            //            foreach (ListItem litem in lstCheckBox.Items)
            //            {
            //                if (litem.Selected)
            //                {
            //                    if (!String.IsNullOrEmpty(data))
            //                        data += AppConfigurer.TicketDataSeparator;
            //                    data += litem.Value;
            //                }
            //            }
            //            break;

            //        case ServiceQuestionDataType.Office:

            //            break;

            //        #endregion

            //        #region other
            //        case ServiceQuestionDataType.ConfirmTemplate:
            //            wuc_ConfirmTemplateDownloadList downloadPanel = ((wuc_ConfirmTemplateDownloadList)subItem.FindControl("downloadPanel"));
            //            data = downloadPanel.GetSelectedConfirmTemplate();
            //            break;

            //        case ServiceQuestionDataType.Date:
            //            wuc_CustomDatePicker datePicker = ((wuc_CustomDatePicker)subItem.FindControl("datePicker"));
            //            data = datePicker.GetSelectedDate();
            //            break;

            //        case ServiceQuestionDataType.String:
            //            TextBox txtBox = (TextBox)subItem.FindControl("txtbox");
            //            data = txtBox.Text;
            //            break;

            //        case ServiceQuestionDataType.NA:
            //        default:
            //            break;
            //        #endregion
            //    }
            //    if (!String.IsNullOrEmpty(data))
            //        lstTicketData.Add(new TicketData() { QuesCode = sevQueCode, Answer = data });
        //    }
        //}

        //foreach (TicketData item in lstTicketData)
        //{
        //    item.TicketNo = "";
        //    item.UpdatedUser = "UAT";
        //    item.UpdatedTime = DateTime.Now;
        //}

        //return lstTicketData;
    }

    #endregion

    #region Repeater event

    protected void rptMain_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ServiceStep sevStep = e.Item.DataItem as ServiceStep;

            if (sevStep != null)
            {
                ((HiddenField)e.Item.FindControl("hfStepNo")).Value = sevStep.StepCode;
                ((Label)e.Item.FindControl("lblHeading")).Text = sevStep.StepCodeUser;
                ((LinkButton)e.Item.FindControl("btnExpand")).Text = sevStep.Description;
            }
        }
    }

    protected void rptChild_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ServiceQuestion sevQue = e.Item.DataItem as ServiceQuestion;

            if (sevQue != null)
            {
                ((Label)e.Item.FindControl("lblTitle")).Text = sevQue.Description;
                ((HiddenField)e.Item.FindControl("hfStepNo")).Value = sevQue.QuesCode;

                CheckBoxList lstCheckBox = ((CheckBoxList)e.Item.FindControl("lstCheckbox"));
                RadioButtonList lstRadioButton = ((RadioButtonList)e.Item.FindControl("lstRadioButton"));
                Label lblElse = ((Label)e.Item.FindControl("lblElse"));
                TicketData td = GetTicketData(sevQue.QuesCode);
                string data = "";
                if(td != null && td.Answer != null) 
                    data = td.Answer;
                IList<string> sdata = null;

                switch (sevQue.DataType)
                {
                    #region related multichoice
                    case ServiceQuestionDataType.List:
                        lstRadioButton.Items.Clear();
                        sdata = data.Split(new string[] { AppConfigurer.TicketDataSeparator }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (var ans in sevQue.Answers)
                        {
                            bool isSelected = false;
                            if (sdata.Contains(ans.Key))
                                isSelected = true;
                            ListItem item = new ListItem()
                            {
                                Text = ans.Value,
                                Value = ans.Key,
                                Selected = isSelected
                            };
                            lstRadioButton.Items.Add(item);
                        }
                        lstRadioButton.Visible = true;
                        break;

                    case ServiceQuestionDataType.Office:

                        break;

                    case ServiceQuestionDataType.EngTeam:
                        lstCheckBox.Items.Clear();
                        sdata = data.Split(new string[] { AppConfigurer.TicketDataSeparator }, StringSplitOptions.RemoveEmptyEntries);
                        if (!String.IsNullOrEmpty(this.hfChargeableCode.Value))
                        {
                            Chargeable chargeable = ChargeableOM.GetChargeable(this.hfChargeableCode.Value, this.hfProfileCode.Value);
                            if (chargeable != null)
                            {
                                IList<Staff> lst = chargeable.Members.Values.Select(x => x.Staff).ToList();
                                lst = lst.OrderBy(x => x.DisplayName).ToList();
                                foreach (Staff obj in lst)
                                {
                                    var staff = StaffDAO.GetObject(obj.StaffNo);
                                    ListItem item = new ListItem()
                                    {
                                        Text = staff.DisplayName,
                                        Value = staff.StaffNo,
                                        Selected = sdata.Contains(staff.UserID)
                                    };
                                    lstCheckBox.Items.Add(item);
                                }
                            }
                        }
                        if (lstCheckBox.Items.Count == 0)
                        {
                            lblElse.Text = "---The engagement team has no member/the chargeable code is not set---";
                            lblElse.Visible = true;
                        }
                        else
                        {
                            lstCheckBox.Visible = true;
                        }
                        break;

                    case ServiceQuestionDataType.CheckBox:
                        lstCheckBox.Items.Clear();
                        sdata = data.Split(new string[] { AppConfigurer.TicketDataSeparator }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (var ans in sevQue.Answers)
                        {
                            ListItem item = new ListItem();
                            item.Text = ans.Value;
                            item.Value = ans.Key;
                            item.Selected = sdata.Contains(ans.Key);
                            lstCheckBox.Items.Add(item);
                        }
                        lstCheckBox.Visible = true;
                        break;

                    #endregion

                    #region other
                    case ServiceQuestionDataType.ConfirmTemplate:
                        //wuc_ConfirmTemplateDownloadList downloadPanel = ((wuc_ConfirmTemplateDownloadList)e.Item.FindControl("downloadPanel"));
                        //downloadPanel.BindData();
                        //downloadPanel.Visible = true;
                        //downloadPanel.SetSelecteValue(data);
                        break;

                    case ServiceQuestionDataType.Date:
                        //wuc_CustomDatePicker datePicker = ((wuc_CustomDatePicker)e.Item.FindControl("datePicker"));
                        //datePicker.ValidationGroup = sevQue.QuesCodeUser;
                        //datePicker.IsMandatory = false;
                        //datePicker.OnOrAfterToday = false;
                        //datePicker.Visible = true;
                        break;

                    case ServiceQuestionDataType.File:
                        lblElse.Text = "---Please provide supporting documents---";
                        lblElse.Visible = true;
                        break;

                    case ServiceQuestionDataType.String:
                        TextBox txtBox = (TextBox)e.Item.FindControl("txtbox");
                        txtBox.Text = data;//sevQue.Answers.FirstOrDefault().Value;
                        txtBox.Visible = true;
                       
                        break;

                    case ServiceQuestionDataType.NA:
                    default:
                        break;

                    #endregion
                }
            }
        }
    }

    protected void rptMain_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            string stepNoVer = ((HiddenField)e.Item.FindControl("hfStepNo")).Value;
            //string[] args = stepNoVer.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries);

            Repeater rptChild = (Repeater)e.Item.FindControl("rptChild");
            LinkButton btnExpand = (LinkButton)e.Item.FindControl("btnExpand");

            if (btnExpand.CommandName.Equals("FirstExpand"))
            {
                //rptChild.DataSource = ServiceTemplateOM.ListServiceQuestions(stepNoVer, Convert.ToInt32(this.hfVersionNo.Value));
                //rptChild.DataBind();
                //btnExpand.CommandName = "Collapse";
                //rptChild.Visible = true;
            }
            else if (btnExpand.CommandName.Equals("Expand"))
            {
                btnExpand.CommandName = "Collapse";
                rptChild.Visible = true;
            }
            else if (btnExpand.CommandName.Equals("Collapse"))
            {
                btnExpand.CommandName = "Expand";
                rptChild.Visible = false;
            }
        }
    }

    protected void rptChild_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {

        }
    }

    protected void lstRadioButton_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadioButtonList lstRadioButton = sender as RadioButtonList;

        if (lstRadioButton == null)
            return;

        string quesAnsCode = lstRadioButton.SelectedValue;
        Control parent = lstRadioButton.Parent;
        HiddenField hfSevQues = parent.FindControl("hfStepNo") as HiddenField;
        Repeater repeater = parent.FindControl("rptSubChild") as Repeater;
        //repeater.DataSource = ServiceTemplateOM.ListServiceQuestionsByParent(hfSevQues.Value, quesAnsCode);
        //repeater.DataBind();
    }

    #endregion
}